package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductTagDTO;
import com.wgh.product.entity.shop.ProductTag;
import com.wgh.product.service.ProductTagService;
import com.wgh.product.converter.ProductTagConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品标签控制器
 * 提供商品标签相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-tag")
public class ProductTagController {

    @Autowired
    private ProductTagService productTagService;

    @Autowired
    private ProductTagConverter productTagConverter;

    /**
     * 获取启用的标签列表
     */
    @GetMapping("/enabled")
    public Result<List<ProductTagDTO>> getEnabledTags() {
        try {
            List<ProductTag> productTags = productTagService.getEnabledTags();
            return Result.success(productTagConverter.toDTOList(productTags));
        } catch (Exception e) {
            return Result.error("获取启用标签列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据标签名称查询标签
     */
    @GetMapping("/name/{name}")
    public Result<ProductTagDTO> getTagByName(@PathVariable String name) {
        try {
            ProductTag productTag = productTagService.getTagByName(name);
            if (productTag == null) {
                return Result.success(null);
            }
            return Result.success(productTagConverter.toDTO(productTag));
        } catch (Exception e) {
            return Result.error("根据名称查询标签失败：" + e.getMessage());
        }
    }

    /**
     * 查询标签关联的商品数量
     */
    @GetMapping("/{id}/product-count")
    public Result<Integer> countProductsByTagId(@PathVariable Long id) {
        try {
            int count = productTagService.countProductsByTagId(id);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("查询标签关联商品数量失败：" + e.getMessage());
        }
    }

    /**
     * 批量更新标签状态
     */
    @PutMapping("/status/batch")
    public Result<Boolean> updateStatusBatch(@RequestParam List<Long> ids, @RequestParam Integer status) {
        try {
            boolean result = productTagService.updateStatusBatch(ids, status);
            return result ? Result.success(true) : Result.error("批量更新标签状态失败");
        } catch (Exception e) {
            return Result.error("批量更新标签状态失败：" + e.getMessage());
        }
    }

    /**
     * 搜索标签
     */
    @GetMapping("/search")
    public Result<List<ProductTagDTO>> searchTags(@RequestParam String keyword) {
        try {
            List<ProductTag> productTags = productTagService.searchTags(keyword);
            return Result.success(productTagConverter.toDTOList(productTags));
        } catch (Exception e) {
            return Result.error("搜索标签失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询标签
     */
    @GetMapping("/page")
    public Result<List<ProductTagDTO>> getTagPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<ProductTag> productTags = productTagService.getTagPage(pageNum, pageSize);
            return Result.success(productTagConverter.toDTOList(productTags));
        } catch (Exception e) {
            return Result.error("分页查询标签失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询标签详情
     */
    @GetMapping("/{id}")
    public Result<ProductTagDTO> getTagById(@PathVariable Long id) {
        try {
            ProductTag productTag = productTagService.getById(id);
            if (productTag == null) {
                return Result.success(null);
            }
            return Result.success(productTagConverter.toDTO(productTag));
        } catch (Exception e) {
            return Result.error("获取标签详情失败：" + e.getMessage());
        }
    }

    /**
     * 批量查询标签
     */
    @GetMapping("/batch")
    public Result<List<ProductTagDTO>> getTagsByIds(@RequestParam List<Long> ids) {
        try {
            List<ProductTag> productTags = productTagService.listByIds(ids);
            return Result.success(productTagConverter.toDTOList(productTags));
        } catch (Exception e) {
            return Result.error("批量查询标签失败：" + e.getMessage());
        }
    }

    /**
     * 新增标签
     */
    @PostMapping
    public Result<Boolean> addTag(@RequestBody ProductTagDTO productTagDTO) {
        try {
            ProductTag productTag = productTagConverter.toEntity(productTagDTO);
            boolean result = productTagService.save(productTag);
            return result ? Result.success(true) : Result.error("新增标签失败");
        } catch (Exception e) {
            return Result.error("新增标签失败：" + e.getMessage());
        }
    }

    /**
     * 更新标签
     */
    @PutMapping
    public Result<Boolean> updateTag(@RequestBody ProductTagDTO productTagDTO) {
        try {
            ProductTag productTag = productTagConverter.toEntity(productTagDTO);
            boolean result = productTagService.updateById(productTag);
            return result ? Result.success(true) : Result.error("更新标签失败");
        } catch (Exception e) {
            return Result.error("更新标签失败：" + e.getMessage());
        }
    }

    /**
     * 删除标签
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteTag(@PathVariable Long id) {
        try {
            boolean result = productTagService.removeById(id);
            return result ? Result.success(true) : Result.error("删除标签失败");
        } catch (Exception e) {
            return Result.error("删除标签失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除标签
     */
    @DeleteMapping("/batch")
    public Result<Boolean> deleteTagsBatch(@RequestParam List<Long> ids) {
        try {
            boolean result = productTagService.removeByIds(ids);
            return result ? Result.success(true) : Result.error("批量删除标签失败");
        } catch (Exception e) {
            return Result.error("批量删除标签失败：" + e.getMessage());
        }
    }
}